<?php
namespace Little\Controller;
use Think\Controller;
class PhoneController extends Controller{

    //获取用户电话
    public function index(){
        $code          = I("code");
        $s             = json_decode(wx($code), true);
        $appid         = APP;
        $sessionKey    = $s['session_key'];
        $encryptedData = I('data');
        $iv            = I('iv');
        $phone         = json_decode($this->decryptData( $appid, $sessionKey,$encryptedData,$iv),true);
        $ph            = $phone['phoneNumber'];
        echo json_encode($ph);
    }
    //发送小程序卡片消息
    public function ka(){
        $code         = I('code');
        $ToUserName   = "gh_8f6ee10da612";//小程序原始id
        $FromUserName = "o2Yv20Fy6citDwpY2hLAUQGFbkpk";//codes($code);       //发送者的openid
        $CreateTime   = time();             //消息创建的时间
        $MsgType      = "miniprogrampage";//
        $MsgId        = 1;                   //消息id 64位整形
        $Title        = 1;                   //标题
        $AppId        = "wxee4c6628f14bec4f";                   //小程序appid
        $PagePath     = "pages/index/index";                   //小程序页面路径
        $ThumbUrl     =1;                    //封面图片的临时cdn链接
        $ThumbMediaId = 1;                   //封面图片的临时素材id
        $access_token = getAccessToken();
        $url          = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$access_token}";

   /*     {
            "touser":"OPENID",
    "msgtype":"miniprogrampage",
    "miniprogrampage":{
            "title":"title",
        "pagepath":"pagepath",
        "thumb_media_id":"thumb_media_id"
    }
}*/

        $data         = json_encode(array("ToUserName"=>$ToUserName,"FromUserName"=>$FromUserName,"CreateTime"=>$CreateTime,"MsgType"=>$MsgType,"MsgId"=>$MsgId,"Title"=>$Title,"AppId"=>$AppId,"PagePath"=>$PagePath,"ThumbUrl"=>$ThumbUrl,"ThumbMediaId"=>$ThumbMediaId));
        //$data         = json_encode(array("touser"=>$FromUserName,"msgtype"=>$MsgType,"miniprogrampage"=>array("title"=>$Title,""=>,""=>)));
        $res          = httpPost($url,$data);
        var_dump($res);
    }
    /**
     * 检验数据的真实性，并且获取解密后的明文.
     * @param $encryptedData string 加密的用户数据
     * @param $iv string 与用户数据一同返回的初始向量
     * @param $data string 解密后的原文
     *
     * @return int 成功0，失败返回对应的错误码
     */
    public function decryptData( $appid,$sessionKey,$encryptedData,$iv,&$data ){
        if (strlen($sessionKey) != 24) {
            return 1;
        }
        $aesKey=base64_decode($sessionKey);
        if (strlen($iv) != 24) {
            return 2;
        }
        $aesIV=base64_decode($iv);
        $aesCipher=base64_decode($encryptedData);
        $result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
        $dataObj=json_decode( $result );
        if( $dataObj  == NULL )
        {
            return 3;
        }
        if( $dataObj->watermark->appid != $appid )
        {
            return 4;
        }
        $data = $result;
        return $data;
    }
}